$(function () {
    var clientTraceDatagrid = $("#client_trace_datagrid");
    var auditForm = $("#audit_form");
    var auditDialog = $("#audit_dialog");
    var clientTraceAddDialog = $("#clientTraceAdd_dialog");
    var clientTraceAddForm = $("#clientTraceAdd_form");
    clientTraceDatagrid.datagrid({
        url:'/clienttrace/list',
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        pagination:true,
        fit:true,
        toolbar:'#clientTrace_toolbar',
        columns:[[
            {field:'client',title:'客户',formatter:formatterClient,width:100},
            {field:'saleman',title:'咨询人员',formatter:formatterSalePeople,width:100},
            {field:'lastTraceTime',title:'本次跟进时间',width:100,formatter:formatterLastTraceTime},
            {field:'nextTraceTime',title:'下次跟进时间',width:100,formatter:formatterNextTraceTime},
            {field:'tracePurpose',title:'跟进目的',width:100,formatter:formatterTracePurpose},
            {field:'communicationMethod',title:'咨询方式',width:100,formatter:formatterCommunicationMethod},
            {field:'qq',title:'QQ',width:100,formatter:formatterQQ},
            {field:'tel',title:'电话',width:100,formatter:formatterTel},
            {field:'intentionDegree',title:'意向程度',width:100,formatter:formatterIntentionDegree},
            {field:'score',title:'审核评分',width:100,formatter:formatterScore},
            {field:'auditor',title:'审核人',width:100,formatter:formatterAuditor},
            {field:'statue',title:'审核状态',width:100,formatter:formatterStatue}
        ]]
    });


    var cmdObj = {

        //刷新
        'reload':function () {
            clientTraceDatagrid.datagrid("reload");
        },
        //打开进审核对话框
        'audit':function () {
            $(":input").prop('disabled','');
            //打开对话框前 先清除表单数据
            auditForm.form("clear");
            //获得当前行对象
            var selectRow = clientTraceDatagrid.datagrid("getSelected");
            console.log(selectRow);
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中想要查看的一行","error");
                return;
            }
            //已经审核不准再审核
            if (selectRow.statue){
                $.messager.alert("提示","请不要重复审核","error");
                return;
            }
            selectRow['client.name']=selectRow.client.name;
            selectRow['saleman.realName']=selectRow.client.saleman.realName;
            selectRow['communicationMethod.name']=selectRow.communicationMethod.name;
            //调用load方法 回填数据
            auditForm.form("load",selectRow);
            //打开对话框
            auditDialog.dialog("open");
        },
        //审核的保存
        'auditSave':function () {
            auditForm.form('submit', {
                url:'/clienttrace/audit',
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success) {
                        $.messager.alert("温馨提示","审核成功","info",function(){
                            //关闭根据对话框
                            cmdObj.auditCancel();
                            //刷新数据列表
                            cmdObj.reload();
                        });
                    } else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //关闭审核对话框
        'auditCancel':function () {
            auditDialog.dialog("close");
        },
        //查看
        'see':function () {
            clientTraceAddForm.form("clear");
            clientTraceAddDialog.dialog({
                buttons:""
            })
            clientTraceAddDialog.dialog({
                buttons:"#see_btns"
            });
            $(":input").prop('disabled','disabled');
            //获得当前行对象
            var selectRow = clientTraceDatagrid.datagrid("getSelected");
            //如果没有行被选中 提示请选择想要编辑的行
            if (!selectRow) {
                $.messager.alert("提示","请选中跟进的对象","error");
                return;
            }

            $.get("/client/getClientById?clientId="+selectRow.client.id,function (data) {
                //将数据回显到表单中
                if (selectRow.communicationMethod){
                    data['communicationMethod.id']=selectRow.communicationMethod.id;
                }
                if (selectRow.tracePurpose){
                    data['tracePurpose.id']=selectRow.tracePurpose.id;
                }
                if (selectRow.consultDuration){
                    data['consultDuration']=selectRow.consultDuration;
                }
                if (selectRow.communicationText){
                    data['communicationText']=selectRow.communicationText;
                }
                if (data.id){
                    data['client.id']=data.id;
                }
                if (data.nextTraceTime){
                    data['client.nextTraceTime']=data.nextTraceTime;
                }
                if (data.saleman){
                    data['saleman.id']=data.saleman.id;
                }
                if (data.bigCustomer) {
                    data['bigCustomer.id']=data.bigCustomer.id;
                }
                if (data.statue){
                    data['statue.id']=data.statue.id;
                }
                if (data.intentionDegree){
                    data['client.intentionDegree.id']=data.intentionDegree.id;
                }
                if (data.clz){
                    data['client.clz.id']=data.clz.id;
                }
                if (data.appointTime){
                    data['client.appointTime']=data.appointTime;
                }
                if (data.clz){
                    data['clz.id']=data.clz.id;
                }
                if (data.intentionDegree){
                    data['intentionDegree.id']=data.intentionDegree.id;
                }
                clientTraceAddForm.form("load",data);
                clientTraceAddDialog.dialog("open");
            },'json');

        },
        //关闭查看对话框
        'seeCancel':function () {
            clientTraceAddDialog.dialog("close");
            //刷新页面
            window.location.href="/clienttrace";
        }


    };


    //统一绑定时间
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    });

})

function formatterClient(value,row,index) {
    console.log(value,row,index);
    return value.name;
}

function formatterSalePeople(value,row,index) {
    return row.client.saleman.realName;
}

function formatterLastTraceTime(value,row,index) {
    return row.client.lastTraceTime;
}

function formatterNextTraceTime(value,row,index) {
    if (row.client.nextTraceTime){
        return row.client.nextTraceTime;
    }
}

function formatterTracePurpose(value,row,index) {
    return value.name;
}

function formatterCommunicationMethod(value,row,index) {
    return value.name;
}

function formatterQQ(value,row,index) {
    if (row.client.qq) {
        return row.client.qq;
    }
}

function formatterTel(value,row,index) {
    if (row.client.qq) {
        return row.client.tel;
    }
}

function formatterIntentionDegree(value,row,index) {
    if (row.client.intentionDegree) {
        return row.client.intentionDegree.name;
    }
}

function formatterScore(value,row,index) {
    if (row.score) {
        return value.name;
    }
}

function formatterAuditor(value,row,index) {
    if (row.auditor) {
        return value.realName;
    }
}

function formatterStatue(value,row,index) {
    if (value) {
        return "<span style='color: green'>已审核</span>"
    } else {
        return "<span style='color: red'>待审核</span>"
    }
}